//
//  mb_SoundListener.c
//  MBAudio
//
//  Created by avansc on 4/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <stdlib.h>
#include <stdio.h>

#include <OpenAL/al.h>

#include "MBAudio.h"

static mb_SoundListener_t *g_SoundListener = NULL;

static void initSoundListener();

static void getListenerPosition(mb_listenerPos *pos);
static void getListenerVelocity(mb_listenerVel *vel);
static void getListenerOrientation(mb_listenerOrientation *ori);

static void setListenerPosition(mb_listenerPos pos);
static void setListenerVelocity(mb_listenerVel vel);
static void setListenerOrientation(mb_listenerOrientation ori);


mb_SoundListener_t *createSoundListener(void)
{
    mb_SoundListener_t *listener = (mb_SoundListener_t*)malloc(sizeof(mb_SoundListener_t));
    
    listener->m_initialized = 0;
 
    listener->init = &initSoundListener;
    
    listener->getPosition = &getListenerPosition;
    listener->getVelocity = &getListenerVelocity;
    listener->getOrientation = &getListenerOrientation;
     
    listener->setPosition = &setListenerPosition;
    listener->setVelocity = &setListenerVelocity;
    listener->setOrientation = &setListenerOrientation;
    
    if(!g_SoundListener)
    {
        setActiveSoundListener(listener);
        initSoundListener();
    }
    
    return listener;
}

mb_SoundListener_t *setActiveSoundListener(mb_SoundListener_t *listener)
{
    mb_SoundListener_t *ret = g_SoundListener;
    g_SoundListener = listener;
    return  ret;
}

void initSoundListener()
{
    if (g_SoundListener->m_initialized == 1)
        return;
    g_SoundListener->m_initialized = 1;
    
}

void getListenerPosition(mb_listenerPos *pos)
{
    alGetListenerfv(AL_POSITION, (ALfloat*)pos);
}

void getListenerVelocity(mb_listenerVel *vel)
{
    alGetListenerfv(AL_VELOCITY, (ALfloat*)vel);
}

void getListenerOrientation(mb_listenerOrientation *ori)
{
    alGetListenerfv(AL_ORIENTATION, (ALfloat*)ori);
}

void setListenerPosition(mb_listenerPos pos)
{
    alListenerfv(AL_POSITION, (const ALfloat*)pos);
}

void setListenerVelocity(mb_listenerVel vel)
{
    alListenerfv(AL_POSITION, (const ALfloat*)vel);
}

void setListenerOrientation(mb_listenerOrientation ori)
{
    alListenerfv(AL_POSITION, (const ALfloat*)ori);
}
